﻿using AnlianCommon.Common.Domains.BankCustomers;
using AnlianCommon.Common.DTO;
using AnlianCommon.Common.Infrastructure.IRepository;
using AnlianCommon.Enum;
using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace SheepCommon.API.Vendors
{
    public class VendorBackHander : IRequestHandler<VendorBackCommand, Result>
    {
        private readonly IBaseRepository<Vendor> _vendorRepository;
        private readonly IMapper _mapper;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="vendorRepository"></param>
        /// <param name="mapper"></param>
        public VendorBackHander(IBaseRepository<Vendor> vendorRepository, IMapper mapper)
        {
            _vendorRepository = vendorRepository;
            _mapper = mapper;
        }

        /// <summary>
        /// 供应商信息回传
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(VendorBackCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Search };
            try
            {
                var personnel = await(await _vendorRepository.GetAll()).Where(c => c.VendorId == request.VendorId).FirstOrDefaultAsync();
                if (personnel != null)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Data = personnel;
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
